isalpha関数は、引数に与えられた文字が英文字かどうかを判定します。
#include <ctype.h>
int isalpha(int c);
cは、チェックしたい文字を指定します。EOF(-1)も判定する可能性があるため、int型になっています。
戻り値として、引数の値が半角英字のASCIIコードであれば0以外を、そうでなければ0を返します。
すでに紹介しているisalnum()の英字のみを判定するのバージョンになります。
プログラム 例
#include <stdio.h>
#include <ctype.h>
#define STR_LEN (10)
int main(void)
{
int loop_cnt;
char str[STR_LEN + 1] = {0};
char in_data;
scanf('%10s', &str);
loop_cnt = 0;
while (str[loop_cnt] != ' ') {
/* 値が英字だったら0以外が返る */
if (isalpha(str[loop_cnt]) != 0) {
break;
}
loop_cnt++;
}
if (loop_cnt < STR_LEN) {
printf('英字が含まれています\n');
}
else {
printf('英字は含まれていません\n');
}
return 0;
}
Copyright © 2011 katsumi Handa All Rights Reserved.